const { errorsModel } = require('../models/errorsModel');

//获取错题
module.exports.getErrors = async function (req, res, next) {
    const id = req.query;
    const result = await errorsModel.find({ studentId: id._id })
        .populate('studentId')
        .populate('exerciseId')
    if (result.length > 0) {
        res.send({
            code: 1,
            message: '获取数据成功',
            result
        })

    } else {
        res.send({
            code: 0,
            message: '获取数据失败',
        })
    }
};

// 创建错题
module.exports.createErrors = async (req, res, next) => {
    const data = await req.body;
    // console.log(data);
    const result = errorsModel.create(data)
    if (result != null) {
        res.send({
            message: '添加成功',
            code: 1
        });
    } else {
        res.send({
            message: '添加失败',
            code: 0
        })
    }
}

//删除错题
module.exports.deleteErrors = async function (req, res, next) {
    const data = await req.body;
    const result = errorsModel.deleteOne(data);
    if (result.deletedCount != 0) {
        res.send({
            message: '删除成功',
            code: 1
        });
    } else {
        res.send({
            message: '删除失败',
            code: 0
        })
    }
}